const AnchorMapper = require("../database/mapper/anchor");
const UserMapper = require("../database/mapper/user");
const {checkCookieField, checkCookieIsExpire} = require("../utils/fun");
const RedPacketBo = require("../bo/red-packet-bo");
const {sleep} = require("ee-bin/tools/serve");
const GameMapper = require("../database/mapper/game");
const ConfigMapper = require("../database/mapper/config");
const ProxyUtils = require("../utils/proxy");
module.exports = class Scanf{
    status = false
    channel = null
    sender = null
    /**
     * @type RedPacketBo
     */
    sentinel = null

    constructor(anchor_id, channel, sender) {
        this.anchorMapper = new AnchorMapper()
        this.userMapper = new UserMapper()
        this.anchor_id = anchor_id
        this.sender = sender
        this.channel = channel
        this.sender.sendMessage = function (message,data = null){
            this.send(channel, {
                message: message,
                id: anchor_id,
                data:data,
            })
        }
    }

    sendMessage(message,data = null){
        this.sender.sendMessage(message,data)
    }

    async run(){
        this.status = true
        const anchor = await this.anchorMapper.read(this.anchor_id)

        let userList = await this.userMapper.getList({
            anchor_id:this.anchor_id,
            game_id:anchor.game_id
        },false)
        let userBoList = await this.getRedPacketBoList(userList,anchor)
        if(!this.sentinel){
            this.status = false
            this.sendMessage("哨兵设置错误")
        }
        await this.setProxy(this.sentinel)
        let silent_gap = anchor.silent_gap * 1000
        let active_gap = anchor.active_gap * 1000
        let time = silent_gap
        let i = 0
        this.sendMessage("开始扫描主播【"+anchor.name + "】的红包")
        while (this.status) {
                if(i!=0){
                        await sleep(time)
                }
                i++
                let {hasRed, isUpdate, maps} = await this.sentinel.getLastList()
                if(hasRed){
                    for (let userBo of userBoList) {
                        await userBo.receiveByMps(maps)
                    }
                }

                time = isUpdate ? active_gap : silent_gap

        }

        this.sendMessage("任务已结束")
        console.log(this.status)
    }

    async getRedPacketBoList(userList,anchor){
        let userBoList = []
        let gameMapper = new GameMapper()
        let game = await gameMapper.find({where: {id: anchor.game_id}})
        for(let user of userList){
                let roleInfo = user.role_fmt_info
                if(!roleInfo){
                    continue
                }
                let bo = new RedPacketBo(
                    roleInfo.game_id,
                    roleInfo.server_id,
                    roleInfo.role_id,
                    roleInfo.user_name)
                bo.setCookies(user.cookies)
                    .setClientIp(user.client_ip)
                    .setDeviceInfo(user.device)
                    .setRegisteredAt(user.registered_at)
                    .setUserId(user.id)
                    .setPwd(anchor.pwd)
                    .setRechargeMoney(user.recharge_money)
                    .setSender(this.sender)
                    .setNetworkDelay(anchor.network_delay)
                    .setOldFansPwd(anchor.old_fans_pwd)

                userBoList.push(bo)
                if(user.id == anchor.sentinel_id){
                    this.sentinel = bo
                }
            setTimeout(async function (){
                await bo.doLoginToLottery(user, game)
            },Math.random()*100)

        }
        return userBoList
    }

    /**
     *
     * @param bo RedPacketBo
     */
    async setProxy(bo){
        let configMapper = new ConfigMapper()
        let isAgent = await configMapper.configs('is_agent')
        if(parseInt(isAgent) === 1){
            let agentUrl = await configMapper.configs('agent_url')
            bo.setProxy(new ProxyUtils(agentUrl))
        }
    }
    stop(){
        this.status = false
        this.sendMessage("关闭任务")
    }

}